package normalisation;

import java.io.IOException;
import java.util.ArrayList;
import normalisation.Normalizer.NormalisationType;

public class NgrammeNorm extends Normalizer {

    private int n;
    

    public NgrammeNorm(EncodageType encodage,
            Boolean filterAccents, Boolean filterPonctuation,
            Boolean filterNombres, Boolean filterStopWords,
            String stopWordsFilePath, Boolean filterOneOccurence,
            Boolean removeTags, Boolean replaceUrl, String replaceUrlBy, int nGramme) {
        super(NormalisationType.Tokenisation, encodage, filterAccents, filterPonctuation, filterNombres,
                filterStopWords, stopWordsFilePath, filterOneOccurence, removeTags,
                replaceUrl, replaceUrlBy);

        n = nGramme;
        // TODO Auto-generated constructor stub
    }

    @Override
    public ArrayList<String> normalize(String text) {

        if (filterAccents) {
            text.replaceAll("[^\\s\\w\\p{Punct}]", "");
        }

        if (filterNombres) {
            text.replaceAll("\\d+", "");
        }
        //Replacage des url avant toute normalisation
        if (replaceUrls) {
            text = remplaceUrlsBy(text, replaceUrlsBy);
        }

        //Suppression des tags (débutent par # ou @)
        if (removeTags) {
            text = removeTags(text);
        }

        if (filterPonctuation) {
            text.replaceAll("\\p{Punct}", "");
        }
        
        if (filterStopWords) {
        	try {
				text = removeStopWords(text);
			} catch (IOException e) {
				e.printStackTrace();
			}
        }

        ArrayList<String> result = new ArrayList<String>();
        result = (new NgrammeTokenizer(n)).tokenize(text);




        //Filtrer des cas particulier
        result = enleverCasParticuliers(result);

        return result;
    }
    
    private String removeStopWords(String text) throws IOException {
    	initStopWordList();
    	for(String w : super.stopWordList) {
    		text = text.replaceAll("\\s"+ w + "\\s", " ");
    	}
    	return text;
    }
}
